package com.smileymedia.smil.stage;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.util.Log;

/**
 * 
 * @author gbugaj
 *
 */
public class Stage {
	private boolean isPlaying;
	private long mPlaybackStart;

	public Stage() {
		mTimelines=new ArrayList<Timeline>();
	}

	public void add(Timeline timeline){		
		mTimelines.add(timeline);
		//update stage duration
		final int size=mTimelines.size();
		double longest=0d;
		for(int i=0;i<size;i++){
			Timeline tl= mTimelines.get(i);
			double start=tl.getStartTime();
			double playTime=tl.getTotalPlayTime();
			double total=start+playTime;
			if(total>longest){
				longest=total;
			}
		}
		mDuration=longest;
	}

	/**
	 * Get duration of the current stage
	 * @return
	 */
	public double getDuration() {	
		return mDuration;
	}



	public List<Timeline>  getTimelines() {
		return mTimelines;
	}

	/**
	 * Return all timelines that have source for download
	 * @return
	 */
	public List<Timeline>  getAllTimelines() {
		List<Timeline> timelines=new ArrayList<Timeline>();

		for(Timeline timeLine:mTimelines){
			if(timeLine instanceof  SequenceTimeline){
				for(Timeline t2:((SequenceTimeline) timeLine).getTimelines()){
					timelines.add(t2);
				}
			}
			else if(timeLine instanceof  ParallelTimeline){
				for(Timeline t2:((ParallelTimeline) timeLine).getTimelines()){
					timelines.add(t2);
				}
			}else{
				timelines.add(timeLine);
			}
		}
		return timelines;
	}


	/**
	 * For debug purpose print out the timelines 
	 */
	public void print() {
		StringBuilder buffer=new StringBuilder();
		int size=mTimelines.size();
		int statePlayTime=(int)this.getDuration();

		for(int i=0;i<statePlayTime;i++){
			buffer.append(" "+i+" ");
		}
		buffer.append("\n");
		for(int i=0;i<statePlayTime;i++){
			buffer.append(" + ");
		}

		for(int i=0;i<size;i++){
			Timeline timeline=mTimelines.get(i);
			buffer.append("\n");
			for(int j=0;j<statePlayTime;j++){
				if(j>=timeline.getStartTime() && j<=timeline.getEndTime()){
					buffer.append(" - ");
				}else
				{
					buffer.append("   ");
				}
			}
			buffer.append("  "+timeline.getStartTime() + "  :: "+timeline.getTotalPlayTime()+ "  :: "+timeline.getContent().getClass().getSimpleName());
		}
		Log.d(TAG, "  "+ buffer.toString());
	}


	public void render(Canvas canvas) {
		//check what state the player is in
		if(isPlaying == false){
			return;
		}
		int size=mTimelines.size();

		int  currentFrame=((int)(System.currentTimeMillis() -  mPlaybackStart))/1000;
		//Log.d(TAG, "  Rendering --"+ System.currentTimeMillis() +" :currentFrame="+currentFrame);

		for(int i=0;i<size;i++){
			Timeline timeline=mTimelines.get(i);
			if(currentFrame>=timeline.getStartTime() && currentFrame<=timeline.getEndTime()){
				timeline.getContent().render(canvas);				
			}
		}
		if(currentFrame > getDuration()){
			isPlaying = false;			
		}
	}

	/**
	 * Start play back of the Message 
	 */
	public void play() {
		Log.d(TAG, "  Playback Started");
		isPlaying=true;
		mPlaybackStart=System.currentTimeMillis();
	}

	//Private 
	private static final String TAG = Stage.class.getSimpleName();	
	private List<Timeline> mTimelines;	
	private double mDuration;



}
